#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "02fun.h"

int operator2num(int left, int right, int (*p)(int, int));

int main(int argc, char **argv)
{
	int res, left, right;
	int (*f)(int, int) = NULL; // 函数指针

	if (argc < 4) {
		printf("命令行参数的个数不足以四则运算\n");
		return 1;
	}

	left = atoi(argv[1]);
	right = atoi(argv[3]);

	if (strcmp(argv[2], "+") == 0) {
		f = add;
	} else if (strcmp(argv[2], "-") == 0) {
		f = sub;
	} else if (strcmp(argv[2], "x") == 0) {
		f = mul;
	} else if (strcmp(argv[2], "/") == 0) {
		f = div2num;
	} else {
		printf("不支持\n");
		return 1;
	}

    // 通过函数指针表明要调用的函数
	printf("%d %s %d = %d\n", left, argv[2], right, operator2num(left, right, f));

	return 0;
}

/*
 对两个整型数做可变的运算
 */
int operator2num(int left, int right, int (*p)(int, int))
{
	// 调用
	return p(left, right);
}







